﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据表 LXK_13_LX_ProductListIn 对应的数据应用功能类。
	/// </summary>
	public class LXK_13_LX_ProductListInApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private ILXK_13_LX_ProductListInRepository service = new LXK_13_LX_ProductListInRepository();

        public List<LXK_13_LX_ProductListInEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<LXK_13_LX_ProductListInEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.ProductionID == keyword);
            }
            return service.FindList(expression, pagination);
        }

        public LXK_13_LX_ProductListInEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public LXK_13_LX_ProductListInEntity GetItem(string ProductionNO, string AssembleLine)
        {
            var expression = ExtLinq.True<LXK_13_LX_ProductListInEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == ProductionNO);
            expression = expression.And(t => t.AssembleLine == AssembleLine);
            return service.FindEntity(expression);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.ProductionID == keyValue);
        }

        public void SubmitForm(LXK_13_LX_ProductListInEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.ProductionID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public bool CheckedEntity(LXK_13_LX_ProductListInEntity entity)
        {
            var expression = ExtLinq.True<LXK_13_LX_ProductListInEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == entity.FProductionNO);
            expression = expression.And(t => t.AssembleLine == entity.AssembleLine);
            //if (!string.IsNullOrEmpty(entity.ProductionID))
            //{
            //    expression = expression.And(t => t.ProductionID != entity.ProductionID);
            //}
            var oldEntityList = service.FindList(expression);
            return oldEntityList.Count == 0;
        }

        public int ResetProductSum(string num)
        {
            string sql = string.Format("UPDATE LXK_13_LX_ProductListIn SET ProductSum{0} = NULL  WHERE  UseUnit='{1}' ", num.PadLeft(3, '0'), UseUnit);
            return service.ExecSql(sql);
        }

        public int SetProductSum(string FProductionNO, List<SimpleClass> valuesList)
        { 
            if(string.IsNullOrEmpty(FProductionNO) || valuesList == null || valuesList.Count <= 0)
            {
                return 0;
            }
            string sql = "UPDATE LXK_13_LX_ProductListIn SET";
            foreach (var item in valuesList)
            {
                sql += " " + item.id + "=" + item.value + ",";
            }
            sql = sql.TrimEnd(',');
            sql += " WHERE FProductionNO='" + FProductionNO + "'";
            return service.ExecSql(sql);
        }
    }
}